package com.tomecode.tools.maven.settings.gui.frm;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.tomecode.tools.maven.settings.gui.images.ImageFactory;
import com.tomecode.tools.maven.settings.gui.utils.GuiUtils;
import com.tomecode.tools.maven.settings.gui.utils.SWTResourceManager;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.grouplayout.GroupLayout;
import org.eclipse.swt.layout.grouplayout.LayoutStyle;

/**
 * Dialog for editing property ( name and value)
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class FrmProperty extends Dialog {

	private String[] result = new String[2];
	protected Shell shlProperty;
	private Text textName;
	private Text textValue;
	// private Properties properties;
	private Label labelWarning;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FrmProperty(Shell parent) {
		super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		setText("Properties");
		// this.properties = properties;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public final String[] open(String name, String value) {
		createContents(name, value);
		shlProperty.open();
		shlProperty.layout();
		Display display = getParent().getDisplay();
		while (!shlProperty.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private final void createContents(String name, String value) {
		shlProperty = new Shell(getParent(), getStyle());
		shlProperty.setImage(ImageFactory.MAVEN_SETTINGS_EDITOR_16);
		shlProperty.setText("Property");
		shlProperty.setSize(633, 152);

		Composite composite = new Composite(shlProperty, SWT.NONE);

		final Button btnSave = new Button(composite, SWT.NONE);

		if (name == null && value == null) {
			shlProperty.setText("New Property");
			btnSave.setEnabled(false);
		} else {
			shlProperty.setText("Edit Property");
			btnSave.setEnabled(true);
		}

		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				result[0] = textName.getText();
				result[1] = textValue.getText();
				shlProperty.close();
			}
		});
		btnSave.setText("Save");

		Group group = new Group(composite, SWT.NONE);
		Label lblName = new Label(group, SWT.NONE);
		lblName.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblName.setText("Name");

		Label lblValue = new Label(group, SWT.NONE);
		lblValue.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD));
		lblValue.setText("Value");

		textName = new Text(group, SWT.BORDER);
		textName.setText(GuiUtils.formateNull(name));
		textName.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				boolean isTextEmpty = textName.getText().trim().length() == 0;
				if (isTextEmpty) {
					labelWarning.setText("Property Name can't be empty");
					btnSave.setEnabled(false);
				} else {
					labelWarning.setText("");
					btnSave.setEnabled(true);
				}
			}
		});

		textValue = new Text(group, SWT.BORDER);
		textValue.setText(GuiUtils.formateNull(value));
		textValue.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
			}
		});

		Button btnCancel = new Button(composite, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				result = null;
				shlProperty.close();
			}
		});
		btnCancel.setText("Cancel");

		labelWarning = new Label(composite, SWT.NONE);
		labelWarning.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD | SWT.ITALIC));
		labelWarning.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GroupLayout gl_shlProperty = new GroupLayout(shlProperty);
		gl_shlProperty.setHorizontalGroup(gl_shlProperty.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE));
		gl_shlProperty.setVerticalGroup(gl_shlProperty.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE));
		GroupLayout gl_composite = new GroupLayout(composite);
		gl_composite.setHorizontalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(labelWarning, GroupLayout.PREFERRED_SIZE, 613, GroupLayout.PREFERRED_SIZE)
				.add(gl_composite.createSequentialGroup().add(427).add(btnSave, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(btnCancel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).add(7))
				.add(GroupLayout.TRAILING, group, GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE));
		gl_composite.setVerticalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite
						.createSequentialGroup()
						.add(gl_composite
								.createParallelGroup(GroupLayout.LEADING)
								.add(gl_composite.createSequentialGroup().add(labelWarning, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE).add(73)
										.add(gl_composite.createParallelGroup(GroupLayout.BASELINE).add(btnCancel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE).add(btnSave, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)))
								.add(group, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)).add(10)));
		GroupLayout gl_group = new GroupLayout(group);
		gl_group.setHorizontalGroup(gl_group.createParallelGroup(GroupLayout.LEADING).add(
				gl_group.createSequentialGroup()
						.add(gl_group.createParallelGroup(GroupLayout.LEADING)
								.add(gl_group.createSequentialGroup().add(lblName, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(textName, GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE))
								.add(gl_group.createSequentialGroup().add(lblValue, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(textValue, GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE))).addContainerGap()));
		gl_group.setVerticalGroup(gl_group.createParallelGroup(GroupLayout.LEADING).add(
				gl_group.createSequentialGroup().add(7)
						.add(gl_group.createParallelGroup(GroupLayout.BASELINE).add(lblName, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE).add(textName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).add(6)
						.add(gl_group.createParallelGroup(GroupLayout.BASELINE).add(lblValue, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE).add(textValue, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addContainerGap()));
		group.setLayout(gl_group);
		composite.setLayout(gl_composite);
		shlProperty.setLayout(gl_shlProperty);

	}
}
